package com.ruoyi.web.controller.business;

import com.ruoyi.business.bo.HcxAppUserAddBo;
import com.ruoyi.business.bo.HcxAppUserEditBo;
import com.ruoyi.business.bo.HcxAppUserQueryBo;
import com.ruoyi.business.service.IHcxAppUserService;
import com.ruoyi.business.vo.HcxAppUserVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户Controller
 *
 * @author wsdz
 * @date 2022-08-17
 */
@Api(value = "用户控制器", tags = {"用户管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/business/AppUser")
public class HcxAppUserController extends BaseController {

    private final IHcxAppUserService iHcxAppUserService;

    /**
     * 查询用户列表
     */
    @ApiOperation("查询用户列表")
    @PreAuthorize("@ss.hasPermi('business:AppUser:list')")
    @GetMapping("/list")
    public TableDataInfo<HcxAppUserVo> list(@Validated HcxAppUserQueryBo bo) {
        return iHcxAppUserService.queryPageList(bo);
    }

    /**
     * 导出用户列表
     */
    @ApiOperation("导出用户列表")
    @PreAuthorize("@ss.hasPermi('business:AppUser:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated HcxAppUserQueryBo bo) {
        List<HcxAppUserVo> list = iHcxAppUserService.queryList(bo);
        ExcelUtil<HcxAppUserVo> util = new ExcelUtil<>(HcxAppUserVo.class);
        return util.exportExcel(list, "用户");
    }

    /**
     * 获取用户详细信息
     */
    @ApiOperation("获取用户详细信息")
    @PreAuthorize("@ss.hasPermi('business:AppUser:query')")
    @GetMapping("/{id}")
    public AjaxResult<HcxAppUserVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iHcxAppUserService.queryById(id));
    }

    /**
     * 新增用户
     */
    @ApiOperation("新增用户")
    @PreAuthorize("@ss.hasPermi('business:AppUser:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody HcxAppUserAddBo bo) {
        return toAjax(iHcxAppUserService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户
     */
    @ApiOperation("修改用户")
    @PreAuthorize("@ss.hasPermi('business:AppUser:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody HcxAppUserEditBo bo) {
        return toAjax(iHcxAppUserService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @PreAuthorize("@ss.hasPermi('business:AppUser:remove')")
    @Log(title = "用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHcxAppUserService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入用户数据
     */
    @ApiOperation("导入用户数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('business:AppUser:import')")
    @Log(title = "用户" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<HcxAppUserVo> util = new ExcelUtil<>(HcxAppUserVo.class);
        List<HcxAppUserVo> hcxAppUserList;
        try {
            hcxAppUserList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iHcxAppUserService.importHcxAppUser(hcxAppUserList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<HcxAppUserVo> util = new ExcelUtil<>(HcxAppUserVo.class);
        return util.importTemplateExcel("用户数据");
    }

    /**
     * 审核用户
     */
    @ApiOperation("审核用户")
    @PreAuthorize("@ss.hasPermi('business:AppUser:audit')")
    @Log(title = "审核用户", businessType = BusinessType.AUDIT)
    @RepeatSubmit
    @PostMapping("/audit")
    public AjaxResult<Void> audit(@Validated @RequestBody HcxAppUserEditBo bo) {
        return toAjax(iHcxAppUserService.audit(bo));
    }

    /**
     * 冻结用户
     */
    @ApiOperation("冻结用户")
    @PreAuthorize("@ss.hasPermi('business:AppUser:dongjie')")
    @Log(title = "冻结用户", businessType = BusinessType.DONGJIE)
    @RepeatSubmit
    @PostMapping("/dongjie")
    public AjaxResult<Void> dongjie(@Validated @RequestBody HcxAppUserEditBo bo) {
        return toAjax(iHcxAppUserService.dongjie(bo));
    }


    /**
     * 取消冻结用户
     */
    @ApiOperation("取消冻结用户")
    @PreAuthorize("@ss.hasPermi('business:AppUser:dongjie')")
    @Log(title = "取消冻结用户", businessType = BusinessType.DONGJIE)
    @RepeatSubmit
    @PostMapping("/cancleDongjie")
    public AjaxResult<Void> cancleDongjie(@Validated @RequestBody HcxAppUserEditBo bo) {
        return toAjax(iHcxAppUserService.cancleDongjie(bo));
    }


    /**
     * 查询用户列表
     */
    @ApiOperation("查询用户列表")
    @PreAuthorize("@ss.hasPermi('business:AppUser:list')")
    @GetMapping("/getAllChildrens")
    public TableDataInfo<HcxAppUserVo> getAllChildrens(@Validated HcxAppUserQueryBo bo) {
        return iHcxAppUserService.queryAllChildrenPageList(bo);
    }


    /**
     * 给用户添加奖励
     */
    @ApiOperation("给用户添加奖励")
    @PreAuthorize("@ss.hasPermi('business:AppUser:add')")
    @Log(title = "给用户添加奖励", businessType = BusinessType.TUIGUANGJIANG)
    @RepeatSubmit
    @PostMapping("/addUserPrice")
    public AjaxResult<Void> addUserPrice(@Validated @RequestBody HcxAppUserEditBo bo) {
        return toAjax(iHcxAppUserService.addUserPrice(bo));
    }
}
